//
//  CDVWebServer.m
//  HelloWorld
//
//  Created by jing zhao on 9/25/14.
//
//

#import "CDVWebServer.h"

@interface CDVWebServer ()
{
    HTTPServer *httpServer;
}
@end
@implementation CDVWebServer

- (void)pluginInitialize
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(serverStarted:)
                                                 name:@"SERVER_STARTED"
                                               object:nil];
}

-(void)startWebServer
{

    // Create server using our custom MyHTTPServer class
	httpServer = [[HTTPServer alloc] init];
	
	// Tell the server to broadcast its presence via Bonjour.
	// This allows browsers such as Safari to automatically discover our service.
	[httpServer setType:@"_http._tcp."];
	
	// Normally there's no need to run our server on any specific port.
	// Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
	// However, for easy testing you may want force a certain port so you can just hit the refresh button.
	// [httpServer setPort:12345];
	
	// Serve files from our embedded Web folder
    

//    NSLog(@"Setting document root: %@", webPath);
#ifdef DEBUG
    NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
#else
    NSString *webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/www"];
#endif
    
    
	NSLog(@"Setting document root: %@", webPath);
	
	[httpServer setDocumentRoot:webPath];
    

    CDVPluginResult* pluginResult = nil;
	NSError *error;
	if([httpServer start:&error])
	{
        NSLog(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
        [[NSNotificationCenter defaultCenter] postNotificationName:KEYPLUGINWEBSERVERSUCCESS
                                                            object:[NSString stringWithFormat:@"http://localhost:%hu/index.html", [httpServer listeningPort]]];
    }
    else
	{
		NSLog(@"Error starting HTTP Server: %@", error);
        NSString *string = [NSString stringWithFormat:@"%@",error];
        [[NSNotificationCenter defaultCenter] postNotificationName:KEYPLUGINWEBSERVERFAILE
                                                            object:string];


    }

}



- (void)startServer:(CDVInvokedUrlCommand*)command
{

    // Create server using our custom MyHTTPServer class
	httpServer = [[HTTPServer alloc] init];
	
	// Tell the server to broadcast its presence via Bonjour.
	// This allows browsers such as Safari to automatically discover our service.
	[httpServer setType:@"_http._tcp."];
	
	// Normally there's no need to run our server on any specific port.
	// Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
	// However, for easy testing you may want force a certain port so you can just hit the refresh button.
	// [httpServer setPort:12345];
	
	// Serve files from our embedded Web folder
	NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
	NSLog(@"Setting document root: %@", webPath);
	
	[httpServer setDocumentRoot:webPath];
    
    // Start the server (and check for problems)
	
    CDVPluginResult* pluginResult = nil;
	NSError *error;
	if([httpServer start:&error])
	{
		NSLog(@"Started HTTP Server on port %hu", [httpServer listeningPort]);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SERVER_STARTED"
                                                            object:[NSString stringWithFormat:@"http://localhost:%hu/index.html", [httpServer listeningPort]]];
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"成功了"];
        [self.commandDelegate sendPluginResult:pluginResult
                                    callbackId:command.callbackId];
	}
	else
	{
		NSLog(@"Error starting HTTP Server: %@", error);
        NSString *string = [NSString stringWithFormat:@"%@",error];
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:string];
        [self.commandDelegate sendPluginResult:pluginResult
                                    callbackId:command.callbackId];
	}




}

- (void) serverStarted:(NSNotification *)notification
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:notification.object]]];
    NSLog(@"加载网页%@", notification.object);
}


@end
